package com.net.oil.user.adapter;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.coupon.bean.OilBusinessDTO;
import com.net.oil.gaode.RoutePlanActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2020/1/14 0014:下午 20:43
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class BusinessListAdapter extends RecyclerView.Adapter<BusinessListAdapter.ViewHolder> {

    private Context context;
    private List<OilBusinessDTO> datas;
    private LayoutInflater inflater;

    public BusinessListAdapter(Context context) {
        this.context = context;
        datas = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    public void updateDatas(List<OilBusinessDTO> datas){
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void updateDatas(OilBusinessDTO data){
        this.datas.add(data);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.business_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final OilBusinessDTO oilBusinessDTO = datas.get(position);
        GlideUtil.getInstance().loadCircleImage(context, holder.shopIc, AppUrl.getImageUrl(oilBusinessDTO.getLogo()), R.drawable.shop_err);

        if (oilBusinessDTO.getName() != null){
            holder.shopName.setText(oilBusinessDTO.getName());
        }

        if (oilBusinessDTO.getAddress() != null){
            holder.shopAddress.setText(oilBusinessDTO.getAddress());
        }

        holder.locationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RoutePlanActivity.class);
                intent.putExtra(Constant.INTENT_LNG, oilBusinessDTO.getLng());
                intent.putExtra(Constant.INTENT_LAT, oilBusinessDTO.getLat());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.shop_ic)
        ImageView shopIc;
        @BindView(R.id.shop_name)
        TextView shopName;
        @BindView(R.id.shop_address)
        TextView shopAddress;
        @BindView(R.id.location_button)
        TextView locationButton;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
